package com.shundr;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.actionbarsherlock.view.Window;
import com.shundr.base.BaseActivity;
import com.shundr.common.util.PrefConstants;
import com.shundr.common.util.PrefUtils;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.util.GetVersionUtil;

/**
 * 启动页
 * SplashActivity
 * 
 * @author larry 2015-4-20 下午6:40:04 
 * @version 1.0.0
 *
 */
public class SplashActivity extends BaseActivity {
	
	private ImageView ivSplash;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		
		if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
			finish();
			return;
		}
		
		ivSplash = (ImageView)findViewById(R.id.iv_splash);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		LContext.width = dm.widthPixels; // 当前屏幕像素
		LContext.height = dm.heightPixels; // 当前屏幕像素

		LayoutParams params = (RelativeLayout.LayoutParams) ivSplash.getLayoutParams();
		params.topMargin = (int) (LContext.height * 0.3);
		params.leftMargin = (int) (LContext.width * 0.2);
		params.rightMargin = (int) (LContext.width * 0.2);
		ivSplash.setLayoutParams(params);
		start();
	}
	
	@Override
	protected void onDestroy() {
		releaseImageView(ivSplash);
		super.onDestroy();
	}



	/**
	 * 打开应用
	 * start(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void start(){
		
		int code = PrefUtils.getPrefInt(mContext, PrefConstants.VERSION_CODE, 0);
		int versionCode=GetVersionUtil.getVersionCode(mContext);
		if(versionCode > code){
			PrefUtils.setPrefBoolean(mContext, PrefConstants.IS_FIRST, true);
		}
		
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				boolean isFirst = PrefUtils.getPrefBoolean(mContext, PrefConstants.IS_FIRST, true);
				if (isFirst) {
					startActivity(new Intent(getApplication(), GuideActivity.class));//轮播图
				} else {
					startActivity(new Intent(getApplication(), MainActivity.class));
				}
				finish();
			}
		}, 3000);
	}
}
